package com.ssl.rms.service.impl;

import com.ssl.rms.entity.Menu;
import com.ssl.rms.entity.MenuExample;
import com.ssl.rms.mapper.MenuMapper;
import com.ssl.rms.service.MenuService;
import com.ssl.rms.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：实现类
 *
 * @author ssl
 * @create 2018/03/07 10:18
 */
@Service
public class MenuServiceImpl implements MenuService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private MenuMapper menuMapper;

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @Override
    public Menu insertSelective(Menu menu) {
        menu.setCreateDate(new Date());
        menu.setUpdateDate(new Date());
        menuMapper.insertSelective(menu);
        return menu;
    }

    /**
     * 逻辑删除
     *
     * @param id
     */
    @Override
    public void deleteByPrimaryKey(Long id) {
        Menu menu = selectByPrimaryKey(id);
        menu.setDelFlag(Constant.DelFlag_T);
        updateByPrimaryKeySelective(menu);
    }

    /**
     * 修改
     *
     * @param menu
     * @return
     */
    @Override
    public Menu updateByPrimaryKeySelective(Menu menu) {
        menu.setUpdateDate(new Date());
        menuMapper.updateByPrimaryKeySelective(menu);
        return menu;
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public Menu selectByPrimaryKey(Long id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        if (null == menu || Constant.DelFlag_T.equals(menu.getDelFlag())) {
            logger.warn(MessageFormat.format("menu id[{0}]-" + Constant.DelFlag_MSG, id));
            return null;
        }
        return menu;
    }

    /**
     * 查询菜单列表
     *
     * @param example
     * @return
     */
    @Override
    public List<Menu> selectByExample(MenuExample example) {
        return menuMapper.selectByExample(example);
    }

    /**
     * 查询所有资源
     *
     * @return
     */
    @Override
    public List<Menu> selectAll() {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(Constant.DelFlag_F);
        return selectByExample(example);
    }


    /**
     * 获取用户拥有的的菜单集合
     *
     * @param map {userId:xx,type:xxx}
     * @return
     */
    @Override
    public List<Menu> selectMenus(Map<String, Object> map) {
        return menuMapper.selectMenus(map);
    }
}
